const {
  getList,
  getDetail,
  newBlog,
  updateBlog,
  delBlog
} = require('../controller/blog');
const {SuccessModal,ErrorModal} = require('../model/resModal');

const handleBlogRouter = (req,res) => {
  const method = req.method;
  //博客列表
  if(method === 'GET' && req.path === '/api/blog/list'){
    const author = req.query.author || '';
    const keyword = req.query.keyword || '';
    const listData = getList(author,keyword);
    
    return new SuccessModal(listData);
  }
  //博客详情
  if(method === 'GET' && req.path === '/api/blog/detail'){
    const id = req.query.id;
    const Data = getDetail(id); 
    // console.log(id)
    return new SuccessModal(Data);
  }

  //新建博客
  if(method === 'POST' && req.path === '/api/blog/new'){
    //  /api/blog/new   {"title":'1111',"content":'2222'}
    const data = newBlog(req.body)
    return new SuccessModal(data);
  }

  //更新博客
  if(method === 'POST' && req.path === '/api/blog/update'){
      // {
      // "id":3,
      // "title":"wang",
      // "content":25
      // }
    const result = updateBlog(req.body)
    console.log(req.body)
    if(result){
      return new SuccessModal()
    }else{
      return new ErrorModal('更新失败');
    }
  }

  //删除博客
  if(method === 'POST' && req.path === '/api/blog/del'){
      const result = delBlog(req.body)//id
      if(result){
        return new SuccessModal()
      }else{
        return new ErrorModal('更新失败');
      }    
  }


}


module.exports = handleBlogRouter
